package com.d3works.apps.cash.management.web;

import com.d3works.apps.account.db.Invoice;
import com.d3works.apps.cash.management.service.InvoiceEstimatedPaymentService;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;

@Singleton
public class InvoiceController {

  public void fillEstimatedPaymentDate(ActionRequest request, ActionResponse response) {
    Invoice invoice = request.getContext().asType(Invoice.class);
    try {
      if (invoice.getDueDate() == null) {
        return;
      }
      invoice =
          Beans.get(InvoiceEstimatedPaymentService.class).computeEstimatedPaymentDate(invoice);
      response.setValues(invoice);

    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }
}
